/** 
 * GetCodeHtml
 * 
 * 03/11/2013
 * 
 * Copyright GPL
 */
package com.jtmproject.javiertejedor.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * This class contains function to get the content of a web-page
 * 
 * @author Javier Tejedor
 */
public class GetCodeHtml {

	/**
	 * this returns the code of the web passed by parameters
	 * @param url
	 * @return
	 */
	public String codeHtml(String url){
		HttpClient httpclient = new DefaultHttpClient(); 
		HttpGet httpget = new HttpGet(url); 
		HttpResponse response;
		
		String resString = null;
		
		try {
			response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity(); 
			InputStream is = entity.getContent(); 
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is, "UTF-8"), 8);
			StringBuffer sb = new StringBuffer();
			String line = null;
			
			while ((line = reader.readLine()) != null) {
				 sb.append(line);
			}
			   
			resString = sb.toString(); 

			is.close(); 
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		return resString;
	}
	
}
